<?php

class BelongsToAssociation extends Association
{

  public function loadData()
  {
    if (! $this->dataLoaded && ! $this->owner->has($this->name)) {
      $model = $this->getModel();

      $id = $this->owner->{$this->getForeignKey()};

      if (! empty($id)) {
        $targetName = Domain::getObject($this->target)->getName();

        $r = new CustomRepository($this->target);

        $obj = $r->find($id);
      }

      if (empty($obj)) {
        $obj = Domain::getObject($this->target)->factory($this->target);
      }

      $this->owner->{$this->name} = $obj;

      $this->dataLoaded = true;
    }
  }

}
